import React, { Component } from 'react'
// import {withRouter} from 'dva/router'
import { routerRedux } from 'dva/router';
import * as api from '../services/example'


class Example extends Component {

  componentDidMount(){
     api.getProduct()
     .then(res=>{
     })
  }

  clickProductList = (e) => {
    const currentProduct = {
      name:'玉米1'
    }
    this.props.dispatch({
      type:'product/updateList',
      payload:currentProduct
    })
  }

  clickProductListAsync = (e) =>{
    const currentProduct = {
      name:'玉米2'
    }
    this.props.dispatch({
      type:'product/updateListAsync',
      payload:currentProduct
    })
  }

  clickProductListHttp = (e) =>{
    console.log('http')
    this.props.dispatch({
      type:'product/updateListHttp',
      payload:{
        id:1001
      }
    })
  }

  // 跳转到首页
  clickPushIndex = (e) =>{
    // this.props.history.push('/')
    this.props.dispatch(routerRedux.push('/'))
  }


  render() {

    const { productList } = this.props.productList
    return (
      <div>
        商品详情：{this.props.title}
        <ul>
          {
            productList.map((item,index)=>{
              return ( <li key={index}>{item.name}</li> ) 
            })
          }
        </ul>

        <button onClick={this.clickPushIndex}>history到首页</button>
        <button onClick={this.clickProductList}>增加</button>
        <button onClick={this.clickProductListAsync}>增加Async</button>
        <button onClick={this.clickProductListHttp}>增加Http</button>
      </div>
    )
  }
}

// export default withRouter(Example)
export default Example